package ig.metrica.ui.modelo;

import ig.metrica.ModeloSBean;
import ig.util.ValueDocument;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
 
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

//import ig.metrica.Modelo;
import ig.metrica.ui.PantallaSevinproPanel;
import ig.util.MetaFabricaFormateadores;
import javax.swing.JComboBox;

public class PantallaDefinicionParametrosRentabilidad extends PantallaSevinproPanel {

	private JFormattedTextField txtFechaAux;
    private JTextField txtFecha;
	private JTextField txtNodias;
	private JRadioButton rad1;
	private JRadioButton rad2;
	private JRadioButton radTIR;
	private JRadioButton radReg;
	private JRadioButton radPre;
	private JRadioButton radPYG;
    private ValueDocument vd= new ValueDocument ("INTEGER");
        //private JlEstadoModeo dmodelo;
    private JComboBox[] comboBoxsColumns=new JComboBox[1];

    @Override
    public JComboBox[] getComboBoxsColumnsTable1() {
        return comboBoxsColumns;
    }

    private JComboBox[] comboBoxsColumnsTable2=new JComboBox[1];
    @Override
    public JComboBox[] getComboBoxsColumnsTable2() {
        return comboBoxsColumnsTable2;
    }
        
	public PantallaDefinicionParametrosRentabilidad(ThreadModelos threadModelos){
            this.threadModelos =threadModelos;
            JPanel panel;
		JPanel panel1;
		JPanel panel2;
//              DefaultFormatterFactory defaultFormatterFactory=null;
//              try{
//                    JFormattedTextField.AbstractFormatter displayFormatter = new MaskFormatter("##/##/####");
//                    defaultFormatterFactory = new DefaultFormatterFactory(displayFormatter, displayFormatter, displayFormatter);
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
//                //Fecha.setFormatterFactory(dff);
 
                
		setLayout( new BorderLayout() );
                vd.setCaracterAceptado("/");
		add( panel = new JPanel(), BorderLayout.NORTH);
			panel.setBorder( BorderFactory.createEmptyBorder(10, 0, 0, 0) );
			panel.setLayout( new GridBagLayout() );
			//----------------------------------------------------------
			panel.add( new JLabel(), new GridBagConstraints(0, 0, 1, 1, 0.4, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
			panel.add( panel1 = new JPanel(), new GridBagConstraints(1, 0, 1, 1, 0.1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
				panel1.setBorder( BorderFactory.createCompoundBorder( 
						BorderFactory.createTitledBorder( BorderFactory.createLineBorder(Color.GRAY), "Fecha Inicial para el Analisis", TitledBorder.LEADING, TitledBorder.TOP ),
						BorderFactory.createEmptyBorder(5,15,5,0) ) );
				panel1.setLayout( new GridBagLayout() );
				//-------------------------------------------------------------------
				panel1.add( rad1 = new JRadioButton(), new GridBagConstraints(0, 0, 1, 1, 0.2, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
				panel1.add( rad2 = new JRadioButton(), new GridBagConstraints(0, 1, 1, 1, 0.2, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
				panel1.add( txtFecha = new JTextField(20), new GridBagConstraints(1, 0, 1, 1, 0.4, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
                                txtFecha.setInputVerifier(MetaFabricaFormateadores.getDateVerifier());
                                txtFecha.setDocument(vd);
                                txtFecha.setToolTipText("Formato:DD/MM/YYYY");
                                
				panel1.add( txtNodias = new JTextField(20), new GridBagConstraints(1, 1, 1, 1, 0.4, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0) );
				txtNodias.setDocument(new ValueDocument ("INTEGER"));
                                panel1.add( new JLabel("  dias  "), new GridBagConstraints(3, 1, 1, 1, 0.4, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
			panel.add( new JLabel("  "), new GridBagConstraints(1, 1, 1, 1, 0.1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
			panel.add( panel2 = new JPanel(), new GridBagConstraints(1, 2, 1, 1, 0.1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
				panel2.setBorder( BorderFactory.createCompoundBorder( 
						BorderFactory.createTitledBorder( BorderFactory.createLineBorder(Color.GRAY), "Tipo de Valoracion en los extremos", TitledBorder.LEADING, TitledBorder.TOP ),
						BorderFactory.createEmptyBorder(5,15,5,0) ) );
				panel2.setLayout( new GridLayout(4,1) );
				//---------------------------------------------------
				panel2.add( radTIR = new JRadioButton("valor a TIR de compra") );
				panel2.add( radReg = new JRadioButton("valor registrado") );
				panel2.add( radPre = new JRadioButton("valor a precio de mercado") );
				panel2.add( radPYG = new JRadioButton("valor registrado en PyG") );
			panel.add( new JLabel(), new GridBagConstraints(2, 0, 1, 1, 0.4, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );

		rad1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){rad1ActionPerformed(e);}});
		rad2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){rad2ActionPerformed(e);}});
			
		ButtonGroup grupo1 = new ButtonGroup();
		grupo1.add( rad1 ); 
		grupo1.add( rad2 );
		//rad1.setSelected(true);		
		
		ButtonGroup grupo2 = new ButtonGroup();
		grupo2.add( radTIR );
		grupo2.add( radReg );
		grupo2.add( radPre );
		grupo2.add( radPYG );
		//radTIR.setSelected(true);
		
	}
	
	public String getFecha(){            
		//return txtFecha.getText().trim();
                int ocurrencias=1;
                String dia="";
                String mes="";
                String anno="";
                for (int i = 0; i < txtFecha.getText().length(); i++) {            
                    if(!txtFecha.getText().substring(i,i+1).equals("/")){
                       if(ocurrencias==1)
                        dia = dia +txtFecha.getText().substring(i,i+1);
                       else if(ocurrencias==2)
                        mes= mes+ txtFecha.getText().substring(i,i+1);
                       else if(ocurrencias==3)
                        anno= anno+ txtFecha.getText().substring(i,i+1);
                    }else
                        ocurrencias++;                
                }
                return mes+"/"+dia+"/"+anno;
	}
	String fechaStirng=null;
        String numDiasString=null;
	public void setFecha(Date fecha){
            if(numDiasString==null){
		rad1.setSelected(true);
                fechaStirng=fecha.getDate() + "/" + (fecha.getMonth()+1) + "/" + (fecha.getYear()+1900);
		txtFecha.setText(fechaStirng);
            }
	}
	
	public String getNodias(){
//		if(txtNodias.getText().trim()!="")
//                    rad2.setSelected(true);
		return txtNodias.getText().trim();                
	}

	public void setNodias(int nodias){
		txtNodias.setText(String.valueOf(nodias));
                numDiasString=String.valueOf(nodias);
                rad2.setSelected(true);
	}
	
	public int getTipoValoracionExtremos(){
		if (radTIR.isSelected())
			return ModeloSBean.VALORACION_TIRCOMPRA;
		else if (radReg.isSelected())
			return ModeloSBean.VALORACION_REGISTRADA;
		else if (radPre.isSelected())
			return ModeloSBean.VALORACION_PRECIOMERCADO;
		else 
			return ModeloSBean.VALORACION_REGISTRADA_PYG;
	}
	
	public void setTipoValoracionExtremos(int tipo){
		if (tipo==ModeloSBean.VALORACION_TIRCOMPRA)
			radTIR.setSelected(true);
		else if (tipo==ModeloSBean.VALORACION_REGISTRADA)
			radReg.setSelected(true);
		else if (tipo==ModeloSBean.VALORACION_PRECIOMERCADO)
			radPre.setSelected(true);
		else if (tipo==ModeloSBean.VALORACION_REGISTRADA_PYG)
			radPYG.setSelected(true);
	}
	
	public void rad1ActionPerformed(ActionEvent e){
		txtFecha.setEnabled( true );
		txtNodias.setEnabled( false );
                numDiasString=txtNodias.getText();
                txtNodias.setText("");
                txtFecha.setText(fechaStirng);
	}

	public void rad2ActionPerformed(ActionEvent e){
		txtNodias.setEnabled( true );
		txtFecha.setEnabled( false );
                fechaStirng=txtFecha.getText();
                txtFecha.setText("");
                txtNodias.setText(numDiasString);                
	}
	
	public String getTitulo() {
		return "Definicion Parametros Rentabilidad";
	}

	public void setModificable(boolean b) {
		//rad1.setEnabled(b);
		//rad2.setEnabled(b);
		
		if (b==false){
			txtFecha.setEnabled(b);
			txtNodias.setEnabled(b);
		}else{
			if (rad1.isSelected()){
                            txtFecha.setEnabled(true);
                        }if (rad2.isSelected()){
                            txtNodias.setEnabled(true);
                        }
		}
                rad1.setEnabled(b);
                rad2.setEnabled(b);
		radTIR.setEnabled(b);
		radReg.setEnabled(b);
		radPre.setEnabled(b);
		radPYG.setEnabled(b);
	}

 	public void limpiarCampos() throws Exception {
		txtFecha.setText("");
		txtNodias.setText("");
		radTIR.setSelected(true);
		rad1.setSelected(true);
                String fechaStirng="";
                String numDiasString="";
                //txtFecha.setDocument(vd);                
	}

    public void salir() throws Exception {
    }
    public javax.swing.table.AbstractTableModel getClonModelTable_1() throws CloneNotSupportedException{
        return null;
    }

    public javax.swing.table.AbstractTableModel getClonModelTable_2() throws CloneNotSupportedException{
        return null;
    }

    public javax.swing.table.AbstractTableModel getClonModelTable_3() throws CloneNotSupportedException{
        return null;
    }

}
